package com.studorm.xml.action;



import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;


import javax.servlet.http.HttpServletRequest;


import org.apache.struts2.ServletActionContext;
import org.dom4j.Document;
import org.dom4j.Node;

import com.opensymphony.xwork2.ActionSupport;
import com.studorm.Connection_mysql;
import com.studorm.MD5;
import com.studorm.XMLOption;


public class EditPWAction extends ActionSupport{
		
	private static final long serialVersionUID = -3697921271744988026L;
			private int identity;
	
	public int getIdentity() {
				return identity;
			}

			public void setIdentity(int identity) {
				this.identity = identity;
			}

	public String editPassword() throws Exception{
		HttpServletRequest request = ServletActionContext.getRequest(); 
		String identity_s=request.getParameter("identity");
		String id = request.getParameter("id");
		String oldpw = request.getParameter("oldpw");
		String newpw = request.getParameter("newpw");
		MD5 Md5 =  new MD5();
		
		
		//HttpServletResponse response = ServletActionContext.getResponse();
		
		if(id == null || id.isEmpty()){
			return ERROR;
		}
		if(oldpw == null || oldpw.isEmpty() || oldpw.length() < 6){
			return ERROR;
		}
		if(newpw == null || newpw.isEmpty() || newpw.length() < 6){
			return ERROR;
		}
		if(identity_s == null | identity_s.isEmpty()){
			return ERROR;
		}
		identity = Integer.parseInt(identity_s);
		
		String studentsinfsPath=request.getSession().getServletContext().getRealPath("\\db\\xml\\studentsinfs.xml");
		Document studentsinfsDoc = XMLOption.loadXML(studentsinfsPath);
		
		String staffinnfsPath=request.getSession().getServletContext().getRealPath("\\db\\xml\\staffinfs.xml");
		Document staffinfsDoc = XMLOption.loadXML(staffinnfsPath);
		int count = 0;	
		if(identity == 3){
			String md5oldpw = Md5.getMD5Str(oldpw);
			Node stupwNode = studentsinfsDoc.selectSingleNode("studentsinfs/studentsinf[@studentNu="+id+"]/password");
			if(md5oldpw.endsWith(stupwNode.getText()))
			{
				count=1;
			}
		}
		if(identity == 2){
				String md5oldpw = Md5.getMD5Str(oldpw);
				Node staffpwNode = staffinfsDoc.selectSingleNode("staffinfs/staffinf[@staffNu="+id+"]/password");
				if(md5oldpw.endsWith(staffpwNode.getText()))
				{
					count=1;
				}
		}
				
		if(count == 1){
			
			if(identity == 3){
				Node stuNode = studentsinfsDoc.selectSingleNode("studentsinfs/studentsinf[@studentNu="+id+"]");
				String md5newpw = Md5.getMD5Str(newpw);
				 stuNode.selectSingleNode("password").setText(md5newpw);
				 XMLOption.writeXml(studentsinfsDoc,studentsinfsPath);
			}
			if(identity == 2){
				Node staffNode = staffinfsDoc.selectSingleNode("staffinfs/staffinf[@staffNu="+id+"]");
				String md5newpw = Md5.getMD5Str(newpw);
				 staffNode.selectSingleNode("password").setText(md5newpw);
				 XMLOption.writeXml(staffinfsDoc,staffinnfsPath);
			}
		
		}
	else{
			//conn.close();
			identity = 0;
		}
		

		return SUCCESS;
	}

}
